<?php

namespace app\queue\stomp;

use app\queue\BaseConsumer;
use common\service\EntityService;
use Webman\Stomp\RetryAbleConsumer;
use common\exception\LogicException;

/**
 * 计算实体状态
 * Class CheckEntityStatusConsumer
 */
class CheckEntityStatusConsumer extends BaseConsumer implements RetryAbleConsumer
{
    // 要消费的队列名
    public $queue = 'check_entity_status';

    // 连接名，对应 config/redis_queue.php 里的连接`
    public $connection = 'default';

    public function doConsume($data)
    {
        $entityId = $data['entity_id'];
        try {
            EntityService::getInstance()->statusCheck($entityId);
        } catch (LogicException $e) {
            APP_DEBUG && trace(format_exception_info($e), 'ERR');
        }
    }

    /**
     * Summary of getTtr
     * @return int
     */
    public function getTtr()
    {
        return 20;
    }

    /**
     * Summary of canRetry
     * @param mixed $attempt
     * @param mixed $error
     * @return bool
     */
    public function canRetry($attempt, $error)
    {
        trace(format_exception_info($error), 'ERR');
        return $attempt <= 3;
    }

}
